#ifndef HTTP_RESPONSE_H
#define HTTP_RESPONSE_H

#include <string>
#include <unordered_map>

class Buffer;

class HttpResponse
{
public:
    enum class Version
    {
        Unknown,
        Http10,
        Http11
    };

    enum class StatusCode
    {
        Unknown,
        Ok200 = 200,
        NoContent204 = 204,
        MovedPermanently301 = 301,
        BadRequest400 = 400,
        Unauthorized401 = 401,
        Forbidden403 = 403,
        NotFound404 = 404,
        Conflict409 = 409,
        InternalServerError500 = 500
    };

    HttpResponse() = default;
    explicit HttpResponse(bool needCloseConnection) : needCloseConnection_(needCloseConnection_)
    {
    }

    void setVersion(Version version)
    {
        this->version_ = version;
    }
    std::string getVersionName() const
    {
        if (this->version_ == Version::Http10)
        {
            return "HTTP/1.0";
        }
        else if (this->version_ == Version::Http11)
        {
            return "HTTP/1.1";
        }
        return "Unknown";
    }

    void setStatusCode(StatusCode statusCode)
    {
        this->statusCode_ = statusCode;
    }

    void setStatusMessage(const std::string &statusMessage)
    {
        this->statusMessage_ = statusMessage;
    }

    void setLine(Version version,
                 StatusCode statusCode,
                 const std::string &statusMessage)
    {
        this->version_ = version;
        this->statusCode_ = statusCode;
        this->statusMessage_ = statusMessage;
    }

    void addHeader(const std::string &key, const std::string &value)
    {
        this->headers_[key] = value;
    }
    void setContentType(const std::string &contentType)
    {
        this->addHeader("Content-Type", contentType);
    }
    void setContentLength(int length)
    {
        this->addHeader("Content-Length", std::to_string(length));
    }
    void setErrorHeader() {}

    void setBody(const std::string &body)
    {
        this->body_ = body;
    }

    void setNeedCloseConnection(bool needCloseConnection)
    {
        this->needCloseConnection_ = needCloseConnection;
    }
    bool getNeedCloseConnection() const
    {
        return this->needCloseConnection_;
    }

    void appendToBuffer(Buffer *buffer) const; // 添加数据到缓冲区

private:
    Version version_ = Version::Http10; // 可以默认Unknown，testHttpServer.cpp回调时设置
    StatusCode statusCode_ = StatusCode::Unknown;
    std::string statusMessage_;
    std::unordered_map<std::string, std::string> headers_;
    std::string body_;

    bool needCloseConnection_ = true;
};

#endif // HTTP_RESPONSE_H